package sv.com.seguros.web.controller.ctg;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.tuckey.web.filters.urlrewrite.utils.StringUtils;

import sv.com.seguros.dto.ctg.CtgPlan;
import sv.com.seguros.exception.WebException;
import sv.com.seguros.web.controller.BaseController;
import sv.com.seguros.web.util.DBContext;
import sv.com.seguros.web.util.WebUtilidades;

@Controller
@RequestMapping("/admin/planes")
public class CtgPlanController extends BaseController{

	private static final long serialVersionUID = 6225391779182578991L;
	
	@RequestMapping(value = "view", method = {RequestMethod.POST})
	public String view(ModelMap model, 
			@RequestParam (required = false) String header) {
		model.addAttribute(WebUtilidades.HEADER_TITLE, StringUtils.isBlank(header) ? "CATALOGO" : header);
		try {
			model.put("data", JSONArray.fromObject(DBContext.getCtgPlanDao().findAllAsArray()));
			model.put("ctgAseguradoras", JSONArray.fromObject(DBContext.getCtgAseguradoraDao().findAllActivesAsArray()));
			model.put("ctgTipoSeguros", JSONArray.fromObject(DBContext.getCtgCatalogoDao().findAllActivesByCodigoAsArray(WebUtilidades.CTG_TIPO_SEGUROS_PADRE)));
		} catch (WebException e) {
			logger.error("view", e);
		}
		return "admin/catalogo/ctgPlan";
	}
	
	@RequestMapping(value = "save", method = {RequestMethod.POST})
	public void save(
			@ModelAttribute CtgPlan ctgPlan,
			HttpServletRequest request,
			HttpServletResponse response)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			DBContext.getBaseDao().save(ctgPlan);
			result.put("data", JSONArray.fromObject(DBContext.getCtgPlanDao().findAllAsArray()));
			result.put("dataIndex", ctgPlan.getCtgPlanId());
			result.put(SUCCESS, true);
			result.put(MESSAGE, UPDATED_DATA);
		} catch (WebException e) {
			logger.error("save", e);
			result.put(FAILURE, true);
			result.put(MESSAGE, e.getExceptionMessage());
		} catch (Exception e) {
			logger.error("save", e);
			result.put(FAILURE, true);
			result.put(MESSAGE, DEFAULT_ERROR_MESSAGE);
		}
		result.put("ctgPlanId", ctgPlan.getCtgPlanId());
		serializeObject(result, response);
	}
	
	@RequestMapping(value = "delete", method = {RequestMethod.POST})
	public void delete(@RequestParam Long ctgPlanId,
			HttpServletResponse response){
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			DBContext.getCtgPlanDao().delete(ctgPlanId);
			result.put("data", JSONArray.fromObject(DBContext.getCtgPlanDao().findAllAsArray()));
			result.put(SUCCESS, true);
			result.put(MESSAGE, UPDATED_DATA);
		} catch (WebException e) {
			logger.error("Exception: ", e);
			result.put(FAILURE, true);
			result.put(MESSAGE, e.getExceptionMessage());
		} catch (Exception e) {
			logger.error("Exception: ", e);
			result.put(FAILURE, true);
			result.put(MESSAGE, DEFAULT_ERROR_MESSAGE);
		}
		serializeObject(result, response);
	}
	
	@RequestMapping(value = "toggle", method = {RequestMethod.POST})
	public void toggle(@RequestParam Long ctgPlanId,
			@RequestParam String ctgPlanActivo,
			HttpServletResponse response){
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			DBContext.getCtgPlanDao().toggleActive(ctgPlanId, ctgPlanActivo);
			result.put("data", JSONArray.fromObject(DBContext.getCtgPlanDao().findAllAsArray()));
			result.put(SUCCESS, true);
			result.put(MESSAGE, UPDATED_DATA);
		} catch (WebException e) {
			logger.error("delete", e);
			result.put(FAILURE, true);
			result.put(MESSAGE, e.getExceptionMessage());
		} catch (Exception e) {
			logger.error("delete", e);
			result.put(FAILURE, true);
			result.put(MESSAGE, DEFAULT_ERROR_MESSAGE);
		}
		serializeObject(result, response);
	}

}
